import axios from "axios";

export const isProduction = process.env.NODE_ENV === "production";

export function Request(
	method = "get",
	url,
	data,
	timeout = 2000,
	baseURL = null,
	headers = {}
) {
	let requestData = {
		/* eslint-disable */
		baseURL: !!baseURL
			? baseURL
			: isProduction
			? "https://www.example.com"
			: "http://172.30.200.148:28082",
		method,
		url,
		data: /get/i.test(method) ? null : data,
		param: /get/i.test(method) ? data : null,
		timeout,
		headers: {
			token: sessionStorage.getItem("token") || null,
			...headers,
		},
	};
	return new Promise((resolve, reject) => {
		axios(requestData)
			.then((res) => {
				if (res.data.code.toString().trim() === "0") {
					resolve();
				} else {
					reject(res.data.msg);
				}
			})
			.catch((err) => {
				reject(err);
			});
	});
}
